﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace XRoute
{
    class DrawingHost : FrameworkElement
    {
        ///////////////////////////////////////////////////////////////////////

        readonly VisualCollection _children;
        readonly DrawingVisual _visual;


        ///////////////////////////////////////////////////////////////////////

        public DrawingHost()
        {
            _children = new VisualCollection(this);
            _visual = new DrawingVisual();

            _children.Add(_visual);
        }

        public void RenderDrawing(Action<DrawingContext> render)
        {
            Contract.Requires(render != null);

            using (var dc = _visual.RenderOpen())
                render(dc);
        }

        public void ClearDrawing()
        {
            using (var dc = _visual.RenderOpen())
            {
                // render nothing
            }
        }
 
        protected override int VisualChildrenCount
        {
            get { return _children.Count; }
        }

        protected override Visual GetVisualChild(int index)
        {
            if (index < 0 || index >= _children.Count)
                throw new ArgumentOutOfRangeException();
 
            return _children[index];
        }
    }
}
